//
//  ViewController.m
//  Topo
//
//  Created by 长进 on 2021/8/9.
//

#import "ViewController.h"
#import "PSBaseTreeGraphView.h"
#import "MyLeafView.h"

#import "ObjCClassWrapper.h"



@implementation ViewController


#pragma mark - Property Accessors

- (void) setRootClassName:(NSString *)newRootClassName
{
    NSParameterAssert(newRootClassName != nil);

    if (![_rootClassName isEqualToString:newRootClassName]) {
        _rootClassName = [newRootClassName copy];

        // Set the orientation of the Graph
        _treeGraphView.treeGraphOrientation  = PSTreeGraphOrientationStyleHorizontal;
        
        // See the enum for more options
        // _treeGraphView.treeGraphOrientation  = PSTreeGraphOrientationStyleHorizontalFlipped;

        // Get an ObjCClassWrapper for the named Objective-C Class, and set it as the TreeGraph's root.
        _treeGraphView.modelRoot = [ObjCClassWrapper wrapperForClassNamed:_rootClassName];
    }
}


#pragma mark - View Creation and Initializer

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void) viewDidLoad
{
    [super viewDidLoad];
    
    UIScrollView *scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:scrollview];
    
    self.treeGraphView = [[PSBaseTreeGraphView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [scrollview addSubview:self.treeGraphView];

    // Set the delegate to self.
    (self.treeGraphView).delegate = self;

    // Specify a .nib file for the TreeGraph to load each time it needs to create a new node view.
    (self.treeGraphView).nodeViewNibName = @"ObjCClassTreeNodeView";

    // Specify a starting root class to inspect on launch.
    
    self.rootClassName = @"UIView";
    
    // The system includes some other abstract base classes that are interesting:
    // [self setRootClassName:@"CAAnimation"];

}


#pragma mark - TreeGraph Delegate

-(void) configureNodeView:(UIView *)nodeView
            withModelNode:(id <PSTreeGraphModelNode> )modelNode
{
    NSParameterAssert(nodeView != nil);
    NSParameterAssert(modelNode != nil);

    // NOT FLEXIBLE: treat it like a model node instead of the interface.
    ObjCClassWrapper *objectWrapper = (ObjCClassWrapper *)modelNode;
    MyLeafView *leafView = (MyLeafView *)nodeView;

    // button
    if ( [objectWrapper childModelNodes].count == 0 ) {
        [leafView.expandButton setHidden:YES];
    }

    // labels
    leafView.titleLabel.text    = objectWrapper.name;
    leafView.detailLabel.text    = [NSString stringWithFormat:@"%zd bytes",
                                   objectWrapper.wrappedClassInstanceSize];

}

@end
